package com.chuanyang.driverh.mainmy;


import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainstart.LoginActivity;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.responseresult.ValidateCodeResult;
import com.chuanyang.driverh.utils.ActivityUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class ChangePasswordActivity extends BaseTitleActivity {

    private EditText et_changeword_account;
    private EditText et_changeword_password;
    private EditText et_changeword_code;
    private EditText et_changeword_password2;
    private TextView tv_get_code;
    private Button btn_change;

    private MyCountDownTimer countDown;
    @Override
    protected int getContentResId() {
        return R.layout.my_act_changepassword;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        et_changeword_account = (EditText) findViewById(R.id.et_changeword_account);
        et_changeword_password = (EditText) findViewById(R.id.et_changeword_password);;
        et_changeword_code = (EditText) findViewById(R.id.et_changeword_code);
        et_changeword_password2 = (EditText) findViewById(R.id.et_changeword_password2);
        tv_get_code = (TextView) findViewById(R.id.tv_get_code);
        btn_change = (Button) findViewById(R.id.btn_change);
//        tv_get_code.setBackgroundResource(R.drawable.round_corner_whitetrans_shape);

    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("修改密码");
    }

    @Override
    protected void initListener() {
        btn_change.setOnClickListener(this);
        tv_get_code.setOnClickListener(this);
    }
    /**
     * 检查
     */
    private boolean checkAll() {


        if (et_changeword_account.getText().toString().length() != 11) {
           PrintUtil.toast(this, "请输入11位手机号");
            return false;
        }
        if (et_changeword_password.getText().toString().length() < 6 || et_changeword_password.getText().toString().length() > 20) {
            PrintUtil.toast(this, "请输入6-20位的密码");
            return false;
        }

        if (et_changeword_password2.getText().toString().length() == 0 || !(et_changeword_password2.getText().toString()).equals(et_changeword_password2.getText().toString())) {
            PrintUtil.toast(this, "请确认两次输入的密码一致");
            return false;
        }
        if (TextUtils.isEmpty(et_changeword_code.getText())) {
            PrintUtil.toast(this, "请输入验证码");
            return false;
        }

        return true;
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_change:
                if (checkAll()) {
                    updatePwdByVCode();
                }
                break;
            case R.id.iv_regist_back:
                finish();
                break;
            case R.id.tv_get_code:
                String phone=et_changeword_account.getText().toString().trim();
                if(StringUtil.isPhoneNumber(phone)){
                    getValidateCode();
//                    getCode(phone);
                }else{
                    PrintUtil.toast(activityContext,"请输入正确的手机号");
                }
                break;
        }
    }

    private void updatePwdByVCode() {
        DialogUtil.showLoadingDialog(activityContext,"修改密码...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("phone", et_changeword_account.getText().toString().trim());
        params.put("password",et_changeword_password.getText().toString().trim());
        params.put("userType", "2");
        params.put("validateCode", et_changeword_code.getText().toString().trim());
        MyHttpUtil.updatePwdByVCode(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    //注册成功之后要处理的
                  PrintUtil.toast(ChangePasswordActivity.this, "修改成功，请重新登录");
                    ActivityUtil.startActivity(activityContext, LoginActivity.class);
                    SharedPrefUtil.setLogined(false);
                    SharedPrefUtil.setPhone("");
                    SharedPrefUtil.setPwd("");
                   onBackPressed();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    private void getValidateCode() {
        DialogUtil.showLoadingDialog(activityContext,"验证码...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("phone", et_changeword_account.getText().toString().trim());
        MyHttpUtil.getCode(activityContext,params, new MyCallback<ValidateCodeResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ValidateCodeResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    startCountdown();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }



    private void startCountdown() {
        countDown = new MyCountDownTimer(90000, 1000);
        countDown.start();
    }

    @Override
    public void finish() {
        super.finish();
        if (countDown != null) countDown.cancel();
    }
    class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onFinish() {
            super.cancel();
            tv_get_code.setText("获取验证码");
//            tv_get_code.setBackgroundResource(R.drawable.round_corner_whitetrans_shape);
            tv_get_code.setEnabled(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_get_code.setText("重新获取" +(millisUntilFinished / 1000) + "s");
            tv_get_code.setEnabled(false);
//            tv_get_code.setBackgroundResource(R.drawable.round_corner_whitegray_shape);
        }
    }

}
